<?php
/**
 * Rsa
 * @author 橘子
 */
namespace app\common\tool;

class Rsa
{
    /**
     * 私钥加密
     * @param type $data 需要加密额字符串长度小于100
     * @param type $private_key 私钥
     * @return type
     */
    public static function privateRncrypt($data, $private_key)
    {
        $encrypted = "";
        openssl_private_encrypt((string) $data, $encrypted, $private_key);
        return base64_encode($encrypted);
    }

    /**
     * 公钥解密
     * @param type $endata 加密后的字符串
     * @param type $public_key 公钥
     * @return string
     */
    public static function publicDecrypt($endata, $public_key)
    {
        $decrypted = "";
        openssl_public_decrypt(base64_decode((string) $endata), $decrypted, $public_key);
        return $decrypted;
    }

    /**
     * 公钥加密
     * @param type $data 需要加密额字符串长度小于100
     * @return string
     */
    public static function publicRncrypt($data, $public_key)
    {
        $encrypted = "";
        openssl_public_encrypt((string) $data, $encrypted, $public_key);
        return base64_encode($encrypted);
    }

    /**
     * 私钥解密
     * @param type $endata 加密后的字符串
     * @return string
     */
    public static function privateDecrypt($endata, $private_key)
    {
        $decrypted = "";
        openssl_private_decrypt(base64_decode((string) $endata), $decrypted, $private_key);
        return $decrypted;
    }

}
